Imports System.Runtime.InteropServices
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports Microsoft.Win32.SafeHandles
Imports System.IO
Imports System.Security.AccessControl
Imports System.Security.Principal
Module FileACL
    ' Adds an ACL entry on the specified directory for the specified account.
    Public Function AddDirectorySecurity(ByVal FileName As String, ByVal Account As String, ByVal Rights As FileSystemRights, ByVal ControlType As AccessControlType) As Boolean
        Try

            ' Create a new DirectoryInfoobject.
            Dim dInfo As New DirectoryInfo(FileName)

            ' Get a DirectorySecurity object that represents the 
            ' current security settings.
            Dim dSecurity As DirectorySecurity = dInfo.GetAccessControl()

            ' Add the FileSystemAccessRule to the security settings. 
            dSecurity.AddAccessRule(New FileSystemAccessRule(Account, Rights, InheritanceFlags.ContainerInherit + InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow))

            ' Set the new access settings.
            dInfo.SetAccessControl(dSecurity)
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "Virtual Desk", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Err.Clear()
            Return False
        End Try
    End Function
    ' Removes an ACL entry on the specified directory for the specified account.
    Public Function RemoveDirectorySecurity(ByVal FileName As String, ByVal Account As String, ByVal Rights As FileSystemRights, ByVal ControlType As AccessControlType) As Boolean
        ' Create a new DirectoryInfo object.
        Try
            Dim dInfo As New DirectoryInfo(FileName)

            ' Get a DirectorySecurity object that represents the 
            ' current security settings.
            Dim dSecurity As DirectorySecurity = dInfo.GetAccessControl()

            ' Add the FileSystemAccessRule to the security settings. 
            dSecurity.RemoveAccessRule(New FileSystemAccessRule(Account, Rights, InheritanceFlags.ContainerInherit + InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow))

            ' Set the new access settings.
            dInfo.SetAccessControl(dSecurity)
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "Virtual Desk", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Err.Clear()
            Return False
        End Try
    End Function
    Public Function AddFileSecurity(ByVal FileName As String, ByVal Account As String, ByVal Rights As FileSystemRights, ByVal ControlType As AccessControlType) As Boolean
        Try

            ' Create a new DirectoryInfoobject.
            Dim dInfo As New FileInfo(FileName)

            ' Get a DirectorySecurity object that represents the 
            ' current security settings.
            Dim dSecurity As FileSecurity = dInfo.GetAccessControl()

            ' Add the FileSystemAccessRule to the security settings. 
            dSecurity.AddAccessRule(New FileSystemAccessRule(Account, Rights, AccessControlType.Allow))
            ''InheritanceFlags.ContainerInherit, PropagationFlags.None, 
            ' Set the new access settings.
            dInfo.SetAccessControl(dSecurity)
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "Virtual Desk", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Err.Clear()
            Return False
        End Try
    End Function

    Public Function RemoveFileSecurity(ByVal FileName As String, ByVal Account As String, ByVal Rights As FileSystemRights, ByVal ControlType As AccessControlType) As Boolean
        ' Create a new DirectoryInfo object.
        Try
            Dim dInfo As New FileInfo(FileName)

            ' Get a DirectorySecurity object that represents the 
            ' current security settings.
            Dim dSecurity As FileSecurity = dInfo.GetAccessControl()

            ' Add the FileSystemAccessRule to the security settings. 
            dSecurity.RemoveAccessRule(New FileSystemAccessRule(Account, Rights, InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow))

            ' Set the new access settings.
            dInfo.SetAccessControl(dSecurity)
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "Virtual Desk", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Err.Clear()
            Return False
        End Try
    End Function

    'Public Sub Set_DirectorySecurity(ByVal FileName As String, ByVal userName As String)
    '    Dim dirName1 As String = FileName
    '    'string userName = System.Environment.MachineName +"\Network Service";
    '    'string userName = System.Environment.MachineName +"\ASPNET";
    '    '' Dim userName As String = "NETWORK SERVICE"
    '    Dim dInfo1 As DirectoryInfo = New DirectoryInfo(dirName1)
    '    Dim dSecurity1 As DirectorySecurity = dInfo1.GetAccessControl()
    '    dSecurity1.AddAccessRule(New FileSystemAccessRule(userName, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit + InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow))
    '    dInfo1.SetAccessControl(dSecurity1)
    'End Sub

End Module
